﻿using System;
using System.Security.Cryptography;
using System.Text;

public static class RSA
{

    private static readonly RSACryptoServiceProvider RSAProvider = new RSACryptoServiceProvider();

    public static string RSAEncrypt(string content, string publickey)
    {
        RSAProvider.FromXmlString(publickey);
        var cipherBytes = RSAProvider.Encrypt(Encoding.UTF8.GetBytes(content), false);
        return Convert.ToBase64String(cipherBytes);
    }


    public static string RSADecrypt(string content, string privatekey)
    {
        try
        {
            RSAProvider.FromXmlString(privatekey);
            var cipherBytes = RSAProvider.Decrypt(Encoding.UTF8.GetBytes(content), false);
            return Convert.ToBase64String(cipherBytes);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
}

